﻿// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
namespace Microsoft.Xrm.Sdk
{
    using System.Runtime.Serialization;
    using Microsoft.Xrm.Sdk.Temporary;
    using AttributeRequiredLevelManagedProperty = Microsoft.Xrm.Sdk.Metadata.AttributeRequiredLevel;

	/// <summary>
	/// Represents an instance of an managed property.
	/// </summary>
	[DataContract(Name = "ManagedProperty{0}", Namespace = XmlNamespaces.V5.Contracts)]
	[KnownType(typeof(BooleanManagedProperty))]
	[KnownType(typeof(AttributeRequiredLevelManagedProperty))]
	public abstract class ManagedProperty<T> : IExtensibleDataObject
	{
		protected ManagedProperty()
			: this(null)
		{
		}

		protected ManagedProperty(string managedPropertyLogicalName)
		{
			_logicalName = managedPropertyLogicalName;

			// default to allowing the attribute to change
			_canBeChanged = true;
		}

		/// <summary>
		/// Value of the attribute that this managed property refers.
		/// </summary>
		[DataMember]
		public T Value
		{
			get { return _value; }
			set { _value = value; }
		}

		/// <summary>
		/// Whether it is valid to change the Value of this managed property.
		/// </summary>
		[DataMember]
		public bool CanBeChanged
		{
			get { return _canBeChanged; }
			set { _canBeChanged = value; }
		}

		/// <summary>
		/// The logical name of the managed property definition.
		/// </summary>
		[DataMember]
		public string ManagedPropertyLogicalName
		{
			get { return _logicalName; }
			internal set { _logicalName = value; }
		}

		private T _value;
		private bool _canBeChanged;
		private string _logicalName;

		#region IExtensibleDataObject Implementation
		private ExtensionDataObject ExtensionDataObject;
		public ExtensionDataObject ExtensionData
		{
			get
			{
				return ExtensionDataObject;
			}
			set
			{
				ExtensionDataObject = value;
			}
		}
		#endregion IExtensibleDataObject Implementation
	}
}
